// pages/detailAddress/detailAddress.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        modalHidden: true,
        modalhidden: true,
        service: '',
        sum: 0,
        commont: '',
        time: '',
        index: 0,
        disTag: false,
        service_items: [],
        pingjiayu: ['店长推荐', '火热预约', '评价极高', '全新服务'],
        pic: ['/icons/nv.png', '/icons/nv1.png', '/icons/nv2.png', '/icons/nv3.png', '/icons/nv4.png'],
        allscore: 5,
        isScore: false,
        score: 0,
        map: true,
        longitude: '112.747106',
        latitude: '37.725375',
        markers: [{
            id: 1,
            longitude: '112.747106',
            latitude: '37.725375',
            title: '晋中市榆次区凤翔街与新聂路交叉路口往西约50米(晋商公园东南侧约50米)'
        }],
       shoucang:false
    },
    onShareAppMessage: function () {
        if (res.from === 'button') {
            console.log(res.target)
          }
          return {
            title: "时间交易所",
            path: '/pages/home/home',
            success: function (res) {
              console.log('成功', res)
              wx.showToast({
                title: '分享成功',
                icon:'none',
                duration:1000
              })
            }
          }

      },
      goshoucang:function(e){
        this.setData({
            shoucang: !this.data.shoucang
        })
        wx.setStorageSync('shoucang', this.data.shoucang)
      },
    jump: function (e) {
        wx.openLocation({
          latitude: parseFloat(this.data.latitude),
          longitude: parseFloat(this.data.longitude),
          name: '时间交易所'
        }) 
      },
    //点击merkers
    markertap(e) {
        console.log(e.markerId)

        wx.showActionSheet({
            itemList: ["腾讯地图","百度地图"],
            success: function (res) {
                console.log(res.tapIndex)
            },
            fail: function (res) {
                console.log(res.errMsg)
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(wx.getStorageSync('latitude') == undefined && wx.getStorageSync('longitude') == undefined){
            wx.getLocation({
                type: 'gcj02',
                success: function (res) {
                  //赋值经纬
                  app.globalData.latitude = res.latitude
                  app.globalData.longitude = res.longitude
                  console.log(app.globalData.latitude)
                  console.log(app.globalData.longitude)
                  wx.setStorageSync('latitude', res.latitude)
                  wx.setStorageSync('longitude', res.longitude)
                }
              })
        }
      
        if (wx.getStorageSync('phone') != '') {
            if(wx.getStorageSync('shoucang') == true){
                this.setData({
                    shoucang:true
                })
            }
           
            let tableName = 'pingjia'

            // 应用查询对象
            let Product = new wx.BaaS.TableObject(tableName)

            // 不设置查询条件
            Product.find().then(res => {
                // success
                console.log(res)
                var sum = 0
                for (var i = 0; i < res.data.objects.length; i++) {
                    var a = Math.floor(Math.random() * 4)
                    res.data.objects[i].img = this.data.pic[a]
                    sum = sum + parseInt(res.data.objects[i].sum)
                    var c = parseInt(res.data.objects[i].sum) / 0.5
                }
                // console.log(res.data.objects.length+1)
                this.setData({
                    pingjia: res.data.objects,
                    allscore: (sum / res.data.objects.length).toFixed(1)
                })
                var b = this.data.allscore / 0.5

                if (b % 2 == 0) {
                    this.setData({
                        man_sum: (b) / 2,
                        ban_sum: 0,
                        mei_sum: 5 - b / 2
                    })
                } else {
                    this.setData({
                        man_sum: (b - 1) / 2,
                        ban_sum: 1,
                        mei_sum: 4 - (b - 1) / 2
                    })
                }

            }, err => {
                // err
            })
        } else {
            wx.showToast({
                title: '请您登录后享受服务',
                icon: 'none',
                duration:1000
            })
            setTimeout(function(e){
                wx.switchTab({
                    url: '/pages/myself/myself',
                  })
            },500)
            
        }

    },
    gomap: function (e) {
        this.setData({
            map: false
        })
    },
    mapConfirm: function (e) {
        this.setData({
            map: true
        })
    },
    mapcancel: function (e) {
        this.setData({
            map: true
        })
    },
    ping: function (e) {
        this.setData({
            modalHidden: false
        })
    },
    fenshu: function (e) {
        if (e.detail.value <= 5 && e.detail.value >= 1) {
            this.setData({
                score: e.detail.value
            })
        } else {
            wx.showToast({
                title: '分数应打在1-5分内哦',
            })
            this.setData({
                score: '5.0'
            })
        }

    },
    xiangqing: function (e) {
        this.setData({
            xiangqing: e.detail.value
        })
    },
    modalConfirm: function (e) {
        this.setData({
            modalHidden: true
        })
        if (this.data.score == '') {
            wx.showToast({
                title: '您未打分哦',
                icon: 'none',
                duration: 1000
            })
        }
        if (this.data.comment == '') {
            wx.showToast({
                title: '您未评论哦',
                icon: 'none',
                duration: 1000
            })
        }
        if (this.data.service == '') {
            wx.showToast({
                title: '请您选择要评论的服务哦',
                icon: 'none',
                duration: 1000
            })
        }
        if (this.data.score != 0 && this.data.comment != '' && this.data.service != '') {
            let tableName = 'pingjia'
            let Product = new wx.BaaS.TableObject(tableName)
            let product = Product.create()
            var date = new Date();
            var mydate = date.getFullYear();
            var mouth = date.getMonth();
            var day = date.getDate()
            // 设置方式一
            let apple = {
                // name: app.globalData.user_name,
                time: mydate + '-' + mouth + '-' + day,
                commont: this.data.comment,
                sum: this.data.score,
                fuwu: this.data.service.name,
                service_time: this.data.service.service_time,
                phone: app.globalData.phone,
                man_sum: this.data.man_da_sum,
                ban_sum: this.data.ban_da_sum,
                mei_sum: this.data.mei_da_sum
                // img:app.globalData.user_img
            }

            product.set(apple).save().then(res => {
                // success
                console.log(res)
            }, err => {
                //err 为 HError 对象
            })
        }


    },
    xianxia: function (e) {
        console.log(e)
        var name = e.currentTarget.dataset.name
        setTimeout(function (e) {
            wx.navigateTo({
                url: '/pages/post-detail/post-detail-service/post-detail-service?id=' + name,
            })
        }, 500)
    },
    comment: function (e) {
        this.setData({
            comment: e.detail.value
        })
    },
    modalCandel: function (e) {
        this.setData({
            modalHidden: true
        })
    },
    fuwu: function (e) {
        this.setData({
            modalHidden: true
        })
        this.setData({
            modalhidden: false
        })
        let tableName = 'Order'

        // 应用查询对象
        let query = new wx.BaaS.Query()

        // 设置查询条件（比较、字符串包含、组合等）
        //...
        query.compare('phone', '=', app.globalData.phone)
        // 应用查询对象
        let Product = new wx.BaaS.TableObject(tableName)
        Product.setQuery(query).find().then(res => {
            // success
            console.log(res.data.objects)

            var fuwu = []
            for (var i = 0; i < res.data.objects.length; i++) {
                if (res.data.objects[i].service_name != undefined) {
                    fuwu.push(res.data.objects[i])
                }
            }
            this.setData({
                fuwu_items: fuwu
            })
            console.log(fuwu.length)
            if (fuwu.length == 0) {
                this.setData({
                    fuwu_items: [{
                        name: '您未完成任何服务哦'
                    }],
                    index: 1
                })
            }
        }, err => {
            // err
        })

    },
    goumai: function (e) {
        this.setData({
            disTag: true
        })
        console.log(app.globalData.service_items)
        for (var i = 0; i < app.globalData.service_items_sum; i++) {
            if (app.globalData.service_items[i].pingjiayu == undefined) {
                app.globalData.service_items[i].pingjiayu = this.data.pingjiayu[0]
            }
        }

        this.setData({
            service_items: app.globalData.service_items
        })
    },
    clear: function (e) {
        this.setData({
            disTag: false
        })
    },
    fenshu2: function (e) {
        var b = this.data.score / 0.5

        if (b % 2 == 0) {
            this.setData({
                isScore: true,
                man_da_sum: (b) / 2,
                ban_da_sum: 0,
                mei_da_sum: 5 - b / 2
            })
        } else {
            this.setData({
                isScore: true,
                man_da_sum: (b - 1) / 2,
                ban_da_sum: 1,
                mei_da_sum: 4 - (b - 1) / 2
            })
        }
    },
    fenshu1: function (e) {
        this.setData({
            isScore: false
        })
    },
    yuyue_xianxia: function (e) {
        wx.navigateTo({
            url: '/pages/post-detail/post-detail-service/post-detail-service?id=' + this.data.service_items[e.currentTarget.dataset.postid].item_name,
        })
    },
    modalhConfirm: function (e) {
        this.setData({
            modalhidden: true
        })
        this.setData({
            modalHidden: false
        })
    },
    bindhcancel: function (e) {
        this.setData({
            modalhidden: true
        })
        this.setData({
            modalHidden: false
        })
    },
    item: function (e) {
        this.setData({
            modalhidden: true,
            service: this.data.fuwu_items[e.currentTarget.dataset.postid]
        })
        this.setData({
            modalHidden: false
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})